package com.wfu.module_home.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wfu.module_base.R;
import com.wfu.module_base.base.BaseActivity;
import com.wfu.module_base.utils.RouteUtils;

public class HomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    public static final String TAG_FRAGMENT_VIDEO = "video";
    public static final String TAG_FRAGMENT_NETVIDEO = "netvideo";
    public static final String TAG_FRAGMENT_AUDIO = "audio";
    public static final String TAG_FRAGMENT_NETAUDIO = "netaudio";

    private RadioGroup rg_bottom_home;
    private Fragment mVideoFragment;

    private Fragment curFragment;
    private Fragment mNetVideoFragment;
    private Fragment mAudioFragment;
    private Fragment mNetAudioFragment;
    private RadioButton mOneRadioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }

    @Override
    public void initResource() {
    }

    @Override
    public void initWidget() {
        rg_bottom_home = getView(R.id.rg_bottom_home);
        rg_bottom_home.setOnCheckedChangeListener(this);
        setDefaultRadioButton();
    }

    /**
     * 默认选中
     */
    private void setDefaultRadioButton() {
        mOneRadioButton = (RadioButton) rg_bottom_home.getChildAt(0);
        mOneRadioButton.setChecked(true);
    }

    /**
     * 切换页面
     *
     * @param checkedId
     */
    private void switchTabPage(int checkedId) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        hideAllFragment(ft);//先把所有的fragment隐藏
        if (checkedId == R.id.rb_video) {
            /**
             * 判null的好处就是，Activity由于配置发生改变回保存之前的状态
             * 下次进来可通过findFragmentByTag和findFragmentById拿到
             */
            mVideoFragment = findFragmentByTag(TAG_FRAGMENT_VIDEO);
            if (mVideoFragment == null) {
                mVideoFragment = RouteUtils.getVideoFragment();
                if (mVideoFragment != null) {
                    ft.add(R.id.fl_content_home, mVideoFragment, TAG_FRAGMENT_VIDEO);//重复添加跑出异常
                }
            }
            curFragment = mVideoFragment;
        } else if (checkedId == R.id.rb_netvideo) {
            mNetVideoFragment = findFragmentByTag(TAG_FRAGMENT_NETVIDEO);
            if (mNetVideoFragment == null) {
                mNetVideoFragment = RouteUtils.getNetVideoFragment();
                if (mNetVideoFragment != null) {
                    ft.add(R.id.fl_content_home, mNetVideoFragment, TAG_FRAGMENT_NETVIDEO);
                }
            }
            curFragment = mNetVideoFragment;
        } else if (checkedId == R.id.rb_audio) {
            mAudioFragment = findFragmentByTag(TAG_FRAGMENT_AUDIO);
            if (mAudioFragment == null) {
                mAudioFragment = RouteUtils.getAudioFragment();
                if (mAudioFragment != null) {
                    ft.add(R.id.fl_content_home, mAudioFragment, TAG_FRAGMENT_AUDIO);
                }
            }
            curFragment = mAudioFragment;
        } else if (checkedId == R.id.rb_netaudio) {
            mNetAudioFragment = findFragmentByTag(TAG_FRAGMENT_NETAUDIO);
            if (mNetAudioFragment == null) {
                mNetAudioFragment = RouteUtils.getNetAudioFragment();
                if (mNetAudioFragment != null) {
                    ft.add(R.id.fl_content_home, mNetAudioFragment, TAG_FRAGMENT_NETAUDIO);
                }
            }
            curFragment = mNetAudioFragment;
        }
        if (curFragment != null) {
            ft.show(curFragment).commit();
        }
    }

    /**
     * 隐藏所有的fragment
     *
     * @param ft
     */
    private void hideAllFragment(FragmentTransaction ft) {
        Fragment fragment;
        fragment = findFragmentByTag(TAG_FRAGMENT_VIDEO);
        if (fragment != null) {
            ft.hide(fragment);
        }
        fragment = findFragmentByTag(TAG_FRAGMENT_NETVIDEO);
        if (fragment != null) {
            ft.hide(fragment);
        }
        fragment = findFragmentByTag(TAG_FRAGMENT_AUDIO);
        if (fragment != null) {
            ft.hide(fragment);
        }
        fragment = findFragmentByTag(TAG_FRAGMENT_NETAUDIO);
        if (fragment != null) {
            ft.hide(fragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switchTabPage(checkedId);
    }
}
